import com.citic.dao.PersonDao;
import mockit.Expectations;
import mockit.Mock;
import mockit.MockUp;
import org.junit.Assert;
import org.junit.Test;

import java.util.Calendar;
import java.util.Date;

public class JmockitDemo {
    public static class PersonDaoMock extends MockUp<PersonDao> {
        @Mock
        public int add() {
            return 12345;
        }

        @Mock
        public int query() {
            return 54321;
        }
        @Mock
        public void print() {
            System.out.println("print1111111");
        }

    }


    @Test
    public void personTest() {
        new PersonDaoMock();
        PersonDao personDao = new PersonDao();
        System.out.println(personDao.add());
        System.out.println(personDao.delete());
    }

    @Test
    public void testRecordConstrutctor1() {
        final Calendar cal = Calendar.getInstance();
        // 把待Mock的类传入Expectations的构造函数，可以达到只mock类的部分行为的目的
        new Expectations(Calendar.class) {
            {
                // 只对get方法并且参数为Calendar.HOUR_OF_DAY进行录制
                cal.get(Calendar.HOUR_OF_DAY);
                result = 7;// 小时永远返回早上7点钟
            }
        };
        Calendar now = Calendar.getInstance();
        // 因为下面的调用mock过了，小时永远返回7点钟了
        Assert.assertTrue(now.get(Calendar.HOUR_OF_DAY) == 7);
        // 因为下面的调用没有mock过，所以方法的行为不受mock影响，
        Assert.assertTrue(now.get(Calendar.DAY_OF_MONTH) == (new Date()).getDate());
    }

}
